<template>
  <div class="page">
    <el-card class="box-card" shadow="never">
      <el-form :model="formdata" label-width="120px">
        <el-form-item label="新闻标题">
          <el-input
            v-model.trim="formdata.title"
            placeholder="新闻标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input
            v-model="formdata.intro"
            placeholder="新闻简介"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input
            v-model.trim="formdata.author"
            placeholder="新闻作者"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="formdata.status" :input="formdata.status">
            <el-radio label="1">有效</el-radio>
            <el-radio label="2">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="内容">
          <!-- <Editor
            api-key="vl7byj8kh7hyvvi6o7137s7khcpfsp762987dlnbcd93u37a"
            v-model="formdata.info"
            :init="init"
          ></Editor> -->
          <ueditor ref="ue" :content="formdata.info"></ueditor>
          <div class="bottom">
            <el-button
              class="btn"
              type="primary"
              @click="submit()"
              v-repeatClick
              >保存</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import ueditor from "@/components/ueditor/myUeditor.vue";
// import Editor from "@tinymce/tinymce-vue";
import { editfromdata } from "@/api/news/index";
import init from "./init";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
// import { partnerNotice_index, partnerNotice_save } from "@/api/shop/shop";
// 通过type判断富文本的类型
// let type = null;
export default {
  data() {
    return {
      // 富文本
      // content: "",

      formdata: {
        title: "",
        intro: "", //简介
        info: "", //内容
        status: "1", //状态
        author: "", //作者
      },
      init,
    };
  },
  props:{
    mydata:{
      type:Object,
      default:{},
    }
  },
  created() {
    this.backfill(this.mydata)
  },

  methods: {
    // 获取富文本的值
    // Uinput(val) {
    //   this.content = val;
    // },
    // 回填
    backfill(data) {
      this.formdata = JSON.parse(JSON.stringify(data));
      this.formdata.status = this.formdata.status.toString();
    },
    submit() {
      this.formdata.info = this.$refs.ue.contents;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.formdata)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.formdata
      );
      editfromdata(data).then((res) => {
        console.log("修改成功", res);
        if (res.errcode == 0) {
          this.$message.success("保存成功");
          // this.$router.push("/news/index");
          setTimeout(()=>{
            this.$emit('close')
          },200)
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },

  beforeDestroy() {
    // this.$bus.$off("Edit-row");
  },
  components: {
    ueditor,
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 100%;
  height: 100vh;
  .bottom {
    background: #fff;
    // box-shadow: 10px 10px 10px #eee;
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    padding: 40px 30px;
    .btn {
      width: 120px;
    }
  }

  .box-card {
    width: 100%;
    margin: 0.875rem;
    height: 100vh;
  }
}
</style>